import Vue from 'vue'

import VueRouter from 'vue-router'

import axios from 'axios'
import VueAxios from 'vue-axios'

import App from './App.vue'
import router from './router'

//导入需要验证token请求路径的列表名单
import { validTokenUrls } from './js/validToken'

//导入flexible设置rem基准值
import 'lib-flexible/flexible'

//导入vant组件
import {
  Swipe,
  SwipeItem,
  Lazyload,
  Image as VanImage,
  Tabbar,
  TabbarItem,
  NavBar,
  Toast,
  Icon,
  Form,
  Field,
  Search,
  Popup,
  NoticeBar,
  Sidebar,
  SidebarItem,
  Button,
  Skeleton,
  Overlay,
  Stepper,
  GoodsAction,
  GoodsActionIcon,
  GoodsActionButton,
  Empty,
  SwipeCell,
  Cell,
  CellGroup,
  Checkbox,
  CheckboxGroup,
  SubmitBar,
  AddressList,
  AddressEdit,
  Tag,
  Card,
  PasswordInput,
  NumberKeyboard,
  Grid,
  GridItem,
  Step,
  Steps,
  Tab,
  Tabs,
  Dialog,
  Uploader,
  CouponCell,
  CouponList,
  PullRefresh,
  ShareSheet,
  RadioGroup,
  Radio,
  DatetimePicker,
  CountDown,

} from 'vant';

//注册vant组件
Vue
  .use(Swipe)
  .use(SwipeItem)
  .use(Lazyload)
  .use(VanImage)
  .use(Tabbar)
  .use(TabbarItem)
  .use(NavBar)
  .use(Toast)
  .use(Icon)
  .use(Form)
  .use(Field)
  .use(Search)
  .use(Popup)
  .use(NoticeBar)
  .use(Sidebar)
  .use(SidebarItem)
  .use(Button)
  .use(Skeleton)
  .use(Overlay)
  .use(Stepper)
  .use(GoodsAction)
  .use(GoodsActionIcon)
  .use(GoodsActionButton)
  .use(Empty)
  .use(SwipeCell)
  .use(Cell)
  .use(CellGroup)
  .use(Checkbox)
  .use(CheckboxGroup)
  .use(SubmitBar)
  .use(AddressList)
  .use(AddressEdit)
  .use(Tag)
  .use(Card)
  .use(PasswordInput)
  .use(NumberKeyboard)
  .use(Grid)
  .use(GridItem)
  .use(Step)
  .use(Steps)
  .use(Tab)
  .use(Tabs)
  .use(Dialog)
  .use(Uploader)
  .use(CouponCell)
  .use(CouponList)
  .use(PullRefresh)
  .use(ShareSheet)
  .use(RadioGroup)
  .use(Radio)
  .use(DatetimePicker)
  .use(CountDown)

Vue.config.productionTip = false

Vue.use(VueAxios, axios)

//设置默认基础请求路径
axios.defaults.baseURL = 'http://www.kangliuyong.com:10002';


// 添加请求拦截器(请求之前触发)
axios.interceptors.request.use(config => {
  // 在发送请求之前做些什么

  // 

  let appkey = 'U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA=';

  let tokenString = sessionStorage.getItem('token');


  if (config.method === 'get') {
    config.params = config.params || {};
    config.params.appkey = appkey;

    if (validTokenUrls.indexOf(config.url) > -1) {
      config.params.tokenString = tokenString;
    }

  } else if (config.method === 'post') {

    //对post请求参数进行序列化 a=1&b=2&....


    config.data = config.data || {};

    if (validTokenUrls.indexOf(config.url) > -1) {
      config.data.tokenString = tokenString;
    }

    let paramsString = '';

    for (let key in config.data) {
      paramsString += `${key}=${config.data[key]}&`;
    }

    paramsString += `appkey=${appkey}`;

    config.data = paramsString;

    // 

  }

  // 


  //启动加载提示
  Toast.loading({
    message: '加载中...',
    duration: 0
  });

  return config;

}, error => {
  // 对请求错误做些什么
  //关闭加载提示
  Toast.clear();
  return Promise.reject(error);
});

// 添加响应拦截器(响应之前触发)
axios.interceptors.response.use(response => {
  // 对响应数据做点什么
  // 

  //关闭加载提示
  Toast.clear();

  return response;
}, error => {
  // 对响应错误做点什么
  //关闭加载提示
  Toast.clear();
  return Promise.reject(error);
});


//防止点击相同路由出现错误(此错误不会后续操作)
let naturalPush = VueRouter.prototype.push;

//重写路由原型的push方法, 在push方法添加catch方法捕获错误
VueRouter.prototype.push = function (location) {
  return naturalPush.call(this, location).catch(err => { });
}

//日期过滤器
Vue.filter('formatDate', value => {
  // 
  if (Object.prototype.toString.call(value) !== "[Object Date]") {
    value = new Date(value);
  }
  var year = value.getFullYear();
  var month = value.getMonth() + 1;
  month = month >= 10 ? month : '0' + month;
  var date = value.getDate();
  date = date >= 10 ? date : '0' + date;
  var hours = value.getHours();
  hours = hours >= 10 ? hours : '0' + hours;
  var minutes = value.getMinutes();
  minutes = minutes >= 10 ? minutes : '0' + minutes;
  var seconds = value.getSeconds();
  seconds = seconds >= 10 ? seconds : '0' + seconds;
  return `${year}-${month}-${date} ${hours}:${minutes}:${seconds}`;
})

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
